//
//  EditInstallAddressViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/20.
//

import UIKit

class EditInstallAddressViewController: BaseViewController {
    var refreshDetailClosure: (()->Void)?
    let customView = CreateJobInstallAddressView(topBGImageName: "createjob_installation_address_top_bg")
    let installAddressViewModel = CreateJobInstallAddressViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initUI()
        self.setData()
    }
    //MARK: - 设置UI
    private func initUI(){
        self.view = self.customView
        self.customView.cancelBtn.isHidden = true//隐藏cancel btn，因为这个View是共用的创建Job的View
        self.customView.setTitleInfo(R.string.localizable.editInstallationAddress())
        self.customView.backToPreBtn.addTarget(self, action: #selector(backToPre), for: UIControl.Event.touchUpInside)
        self.customView.continueBtn.setTitle(R.string.localizable.save(), for: UIControl.State.normal)
        //继续下一步proper owner
        self.customView.continueClosure = {[weak self] in
            guard let strongSelf = self else { return  }
            strongSelf.getData()
            strongSelf.saveInstallAddress()
        }
    }
    //MARK: - 设置数据
    private func setData(){
        let installAddress: InstallationDetailInstallAddress = JobDetailModelHelper.shared.jobDetailModel.install_address
        let ownerAddress: InstallationDetailOwnerAddress = JobDetailModelHelper.shared.jobDetailModel.owner_address
        //install address
        installAddressViewModel.installAddress.address_id = installAddress.address_id
        installAddressViewModel.installAddress.full_address = installAddress.installation_address
        installAddressViewModel.installAddress.unit_type = installAddress.unit_type_value
        installAddressViewModel.installAddress.unit_type_name = installAddress.unit_type
        installAddressViewModel.installAddress.unit_number = installAddress.unit_number
        installAddressViewModel.installAddress.street_type = installAddress.street_type_value
        installAddressViewModel.installAddress.street_type_name = installAddress.street_type
        installAddressViewModel.installAddress.street_number = installAddress.street_number
        installAddressViewModel.installAddress.street_name = installAddress.street_name
        installAddressViewModel.installAddress.state = installAddress.state
        installAddressViewModel.installAddress.suburb = installAddress.suburb
        installAddressViewModel.installAddress.post_code = installAddress.post_code
        //special address
        if installAddress.special_address.trimmingCharacters(in: .whitespaces) != "" {
            installAddressViewModel.addSpecialAddressFlag = true
            installAddressViewModel.specialAddress = installAddress.special_address
        }
        //owner address
        installAddressViewModel.sameAsPostalAddress = installAddress.same_as_postal
        if installAddress.same_as_postal {//same as postal address
            installAddressViewModel.postalAddress.address_type = .physical
            installAddressViewModel.postalAddress.physicalAddress.unit_type = installAddressViewModel.installAddress.unit_type
            installAddressViewModel.postalAddress.physicalAddress.unit_type_name = installAddressViewModel.installAddress.unit_type
            installAddressViewModel.postalAddress.physicalAddress.unit_number = installAddressViewModel.installAddress.unit_number
            installAddressViewModel.postalAddress.physicalAddress.street_type = installAddressViewModel.installAddress.street_type
            installAddressViewModel.postalAddress.physicalAddress.street_type_name = installAddressViewModel.installAddress.street_type
            installAddressViewModel.postalAddress.physicalAddress.street_number = installAddressViewModel.installAddress.street_number
            installAddressViewModel.postalAddress.physicalAddress.street_name = installAddressViewModel.installAddress.street_name
            installAddressViewModel.postalAddress.physicalAddress.state = installAddressViewModel.installAddress.state
            installAddressViewModel.postalAddress.physicalAddress.suburb = installAddressViewModel.installAddress.suburb
            installAddressViewModel.postalAddress.physicalAddress.post_code = installAddressViewModel.installAddress.post_code
        }else{//not same as postal address
            if let addressType = AddressType(rawValue: ownerAddress.address_type_value){
                installAddressViewModel.postalAddress.address_type = addressType
                if  addressType == .physical {//physical
                    installAddressViewModel.postalAddress.physicalAddress.unit_type = ownerAddress.unit_type_value
                    installAddressViewModel.postalAddress.physicalAddress.unit_type_name = ownerAddress.unit_type
                    installAddressViewModel.postalAddress.physicalAddress.unit_number = ownerAddress.unit_number
                    installAddressViewModel.postalAddress.physicalAddress.street_type = ownerAddress.street_type_value
                    installAddressViewModel.postalAddress.physicalAddress.street_type_name = ownerAddress.street_type
                    installAddressViewModel.postalAddress.physicalAddress.street_number = ownerAddress.street_number
                    installAddressViewModel.postalAddress.physicalAddress.street_name = ownerAddress.street_name
                    installAddressViewModel.postalAddress.physicalAddress.state = ownerAddress.state
                    installAddressViewModel.postalAddress.physicalAddress.suburb = ownerAddress.suburb
                    installAddressViewModel.postalAddress.physicalAddress.post_code = ownerAddress.post_code
                }else{//po box
                    installAddressViewModel.postalAddress.delivery_type = ownerAddress.delivery_type
                    installAddressViewModel.postalAddress.delivery_number = ownerAddress.delivery_number
                    installAddressViewModel.postalAddress.state = ownerAddress.state
                    installAddressViewModel.postalAddress.suburb = ownerAddress.suburb
                    installAddressViewModel.postalAddress.post_code = ownerAddress.post_code
                }
            }
        }
        self.customView.setData(self.installAddressViewModel)
    }
    
    fileprivate func getData(){
        let addressModel = CreateJobModelHelper.shared.createJobViewModel.installationAddressViewModel.installAddress
        //不一致就重新赋值
        if addressModel.full_address != addressModel.final_full_address {
            addressModel.full_address = addressModel.final_full_address
            addressModel.address_id = ""
        }
    }
    //MARK: - 保存install address
    func saveInstallAddress(){
        let service = JobDetailService()
        service.setBlockAfterNetwork { [weak self](result, _) in
            guard let strongSelf = self else {return}
            ModelViewUtil.shared.hideUpdatingView()
            let oldState = JobDetailModelHelper.shared.jobDetailModel.install_address.state
            var newState = ""
            if let m = result as? PVDDetailModel{
                newState = m.install_address.state
                JobDetailModelHelper.shared.jobDetailModel.install_address = m.install_address
                JobDetailModelHelper.shared.jobDetailModel.owner_address = m.owner_address
            }
            //如果state从wa改成其他，或从其他改成wa，都刷新数据
            if oldState != newState && (oldState == "WA" || newState == "WA"){
                strongSelf.refreshDetailClosure?()//安装地址的state的修改影响到影响noc的必填项属性，所以要及时刷新
            }
            strongSelf.navigationController?.popViewController(animated: true)
        } errorBlock: { (code, error) in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showErrorInfo(code, error)
        }
        ModelViewUtil.shared.showLoadingView(R.string.localizable.updating())
        service.changeInstallationAddress(self.installAddressViewModel)

    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
}
